package org.chen.controller;

import org.chen.model.dto.ArticleTagDTO;
import org.chen.model.mongodb.ArticleTag;
import org.chen.model.quey.TagQueryParam;
import org.chen.response.Page;
import org.chen.response.Result;
import org.chen.service.impl.TagManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tags")
public class TagController {

    @Autowired
    private TagManagementService tagManagementService;

    @GetMapping
    public Result<Page<ArticleTag>> getTags(TagQueryParam queryParam) {
        return Result.success(tagManagementService.getTags(queryParam));
    }

    @GetMapping("/refresh")
    public Result<Page<ArticleTag>> getTagsRefresh() {
        return Result.success(tagManagementService.refresh());
    }

    @PostMapping
    public Result<ArticleTag> addTag(@RequestBody @Validated ArticleTagDTO tagDTO) {
        return Result.success(tagManagementService.addTag(tagDTO));
    }

    @PutMapping("/{id}")
    public Result<ArticleTag> updateTag(@PathVariable String id,
                                      @RequestBody @Validated ArticleTagDTO tagDTO) {
        return Result.success(tagManagementService.updateTag(id, tagDTO));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteTag(@PathVariable String id) {
        tagManagementService.deleteTag(id);
        return Result.success();
    }
}
